/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import Business.Supplier_BS;
import java.beans.Statement;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Storm^^Rain
 */
public class Supplier_DAO extends ConnectionDB{
    Connection conn= null;
    boolean flag = false;

    public static boolean insert(Business.Supplier_BS s){
        String sql = "{call spInsertSupplier(?,?,?,?,?,?,?,?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);

            cs.setString(1, s.getsName());
            cs.setString(2, s.getsfullName());
            cs.setString(3, s.getsPhone());
            cs.setString(4, s.getsFax());
            cs.setInt(5, s.getsType());
            cs.setString(6, s.getsAddress());
            cs.setString(7, s.getsEmail());
            cs.setString(8, s.getsWebsite());
            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            return false;
        }
    }
    public static boolean update(Business.Supplier_BS s){
        String sql = "{call spUpdateSupplier(?,?,?,?,?,?,?,?,?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);
            cs.setInt(1, s.getsID());
            cs.setString(2, s.getsName());
            cs.setString(3, s.getsfullName());
            cs.setString(4, s.getsPhone());
            cs.setString(5, s.getsFax());
            cs.setInt(6, s.getsType());
            cs.setString(7, s.getsAddress());
            cs.setString(8, s.getsEmail());
            cs.setString(9, s.getsWebsite());

            
            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            return false;
        }
    }
        public static boolean delete(int supplierID){
        String sql = "{call spDeleteSupplier(?)}";
        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);

            cs.setInt(1,supplierID);

            return (cs.executeUpdate()==1);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
            return false;
        }
    }
        public static ArrayList<Business.Supplier_BS> getSuppliers(int supplierType, String name){
        String sql = "{call spSearchSuppliers(?,?)}";
        ArrayList<Business.Supplier_BS> slst = new ArrayList<Business.Supplier_BS>();

        try{
            CallableStatement cs = ConnectionDB.getConnection().prepareCall(sql);
        cs.setInt(1, supplierType);
            cs.setString(2, name);
            
            ResultSet rsSupplier = cs.executeQuery();
            while(rsSupplier.next()){
                Business.Supplier_BS s = new Business.Supplier_BS();
                s.setsID(rsSupplier.getInt("sID"));
                s.setsName(rsSupplier.getString("sName"));
                s.setsfullName(rsSupplier.getString("sfullName"));
                s.setsPhone(rsSupplier.getString("sPhone"));
                s.setsFax(rsSupplier.getString("sFax"));
                s.setsType(rsSupplier.getInt("sType"));
                s.setsAddress(rsSupplier.getString("sAddress"));
                s.setsEmail(rsSupplier.getString("sEmail"));
                s.setsWebsite(rsSupplier.getString("sWebsite"));
                slst.add(s);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
            return slst;
        }

        public List<Supplier_BS> getAllSuppliers() {
        ArrayList<Supplier_BS> lst = new ArrayList<Supplier_BS>();
        conn = Supplier_DAO.getConnection();
        try {
            java.sql.Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("EXEC spGetAllSuppliers");
            while(rs.next()) {
                int id = rs.getInt("sID");
                String name = rs.getString("sName");
                lst.add(new Supplier_BS(id, name));
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return lst;
    }
}
